﻿Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Windows.Forms

Public Class csValidaciones
#Region "ValidacionesUtiles"
    Public Shared Function esRFCValidoPersonaFisica(ByVal sRFC As String) As Boolean
        Return Regex.IsMatch(sRFC, "^[a-zA-ZñÑ]{4,4}(([0-9]{2})([0][13456789]|[1][012])([0][1-9]|[12][\d]|[3][0])|([0-9]{2})([0][13578]|[1][02])([0][1-9]|[12][\d]|[3][01])|([02468][048]|[13579][26])([0][2])([0][1-9]|[12][\d])|([0-9]{2})([0][2])([0][1-9]|[1][\d]|[2][0-8]))(\w{2}[A|a|0-9]{1})$")
    End Function

    Public Shared Function esRFCValidoPersonaMoral(ByVal sRFC As String)
        Return Regex.IsMatch(sRFC, "^[a-zA-Z&ñÑ]{3,3}(([0-9]{2})([0][13456789]|[1][012])([0][1-9]|[12][\d]|[3][0])|([0-9]{2})([0][13578]|[1][02])([0][1-9]|[12][\d]|[3][01])|([02468][048]|[13579][26])([0][2])([0][1-9]|[12][\d])|([0-9]{2})([0][2])([0][1-9]|[1][\d]|[2][0-8]))(\w{2}[A|a|0-9]{1})$")
    End Function

    Public Shared Function esCorreoValido(ByVal sCorreo As String) As Boolean
        Return Regex.IsMatch(sCorreo, "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@" & _
                                        "[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\." & _
                                        "[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$", RegexOptions.IgnoreCase)
    End Function

    Public Shared Function esCodigoPostalValido(ByVal pCP As String) As Boolean
        Return Regex.IsMatch(pCP, "^\d{5}$")
    End Function

    Public Shared Function esCurpValido(ByVal pCurp As String) As Boolean
        Return Regex.IsMatch(pCurp, "^[A-Z][A,E,I,O,U,X][A-Z]{2}[0-9]{2}[0-1][0-9][0-3][0-9][M,H][A-Z]{2}[B,C,D,F,G,H,J,K,L,M,N,Ñ,P,Q,R,S,T,V,W,X,Y,Z]{3}[0-9,A-Z][0-9]", _
                            RegexOptions.IgnoreCase)
    End Function

    Public Shared Function esSerieValida(ByVal pSerie As String) As Boolean
        Return Regex.IsMatch(pSerie, "[a-zA-ZñÑ]{1,25}$")
    End Function

    Public Shared Function esNumeroDeCertificadoValido(ByVal pNumCertif As String) As Boolean
        Return Regex.IsMatch(pNumCertif, "^\d{20}$")
    End Function

    Public Shared Function tieneSoloDigitosYGuiones(ByVal pCadena As String) As Boolean
        Return Regex.IsMatch(pCadena, "^[\d-]+$")
    End Function

    Public Shared Function esContraseñaSegura(ByVal sPwd As String) As Boolean
        'Una Contraseña segura debe ser de 8 caracteres o más, 
        'e incluir al menos 1 Mayúscula, 1 Minúscula, 1 Número y 1 caracter especial como @ # $ ^ & + =
        Return Regex.IsMatch(sPwd, "^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$^&+=]).{8,20}$")
    End Function
#End Region


    Public Shared Function valida_RFC(ByVal sRFC As String, ByVal sTipo As String) As Boolean
        If (sTipo = "F") Then
            Return Regex.IsMatch(sRFC, "^[a-zA-Z]{4,4}(\d{6})((\D|\d){3})?$")
        ElseIf (sTipo = "M") Then
            Return Regex.IsMatch(sRFC, "^[a-zA-Z]{3,3}(\d{6})((\D|\d){3})?$")
        ElseIf (sTipo = "T") Then
            Return Regex.IsMatch(sRFC, "^[a-zA-Z]{3,4}(\d{6})((\D|\d){3})?$")
        Else
            Return False
        End If
    End Function
    Public Shared Function valida_CURP(ByVal sCURP As String) As Boolean
        Dim re As Regex = New Regex( _
                            "^[A-Z][A,E,I,O,U,X][A-Z]{2}[0-9]{2}[0-1][0-9][0-3][0-9][M,H][A-Z]{2}[B,C,D,F,G,H,J,K,L,M,N,Ñ,P,Q,R,S,T,V,W,X,Y,Z]{3}[0-9,A-Z][0-9]", _
                            RegexOptions.IgnoreCase)
        Dim m As Match = re.Match(sCURP)
        If m.Captures.Count = 0 Then
            Return False
        Else
            Return True
        End If
    End Function
    Public Shared Function valida_CLABE(ByVal sCLABE As String) As Boolean
        Dim re As Regex = New Regex( _
                            "^[0-9]{18}", _
                            RegexOptions.IgnoreCase)
        Dim m As Match = re.Match(sCLABE)
        If m.Captures.Count = 0 Then
            Return False
        Else
            Return True
        End If
    End Function
    Public Shared Function valida_Banco_Per_Dec(ByVal sDato As String) As Boolean
        Dim re As Regex = New Regex( _
                            "^[0-9]{3}", _
                            RegexOptions.IgnoreCase)
        Dim m As Match = re.Match(sDato)
        If m.Captures.Count = 0 Then
            Return False
        Else
            Return True
        End If
    End Function
    Public Shared Function valida_URL(ByVal sUrl As String) As Boolean
        Dim re As Regex = New Regex( _
                            "^(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]", _
                            RegexOptions.IgnoreCase)
        Dim m As Match = re.Match(sUrl)
        If m.Captures.Count = 0 Then
            Return False
        Else
            Return True
        End If
    End Function
    Public Shared Function valida_archivoExiste(ByVal sRuta As String, ByVal extension As String) As Boolean
        If (File.Exists(sRuta)) Then
            If (System.IO.Path.GetExtension(sRuta).ToLower = "." & extension.ToLower) Then
                Return True
            Else
                Return False
            End If
        Else
            Return False
        End If
    End Function
    'Public Shared Function valida_RFC2(ByVal sRFC As String, ByVal sTipo As String) As Boolean
    '    If (sTipo = "F") Then
    '        Return Regex.IsMatch(sRFC, "^[a-zA-ZñÑ]{4,4}(([0-9]{2})([0][13456789]|[1][012])([0][1-9]|[12][\d]|[3][0])|([0-9]{2})([0][13578]|[1][02])([0][1-9]|[12][\d]|[3][01])|([02468][048]|[13579][26])([0][2])([0][1-9]|[12][\d])|([0-9]{2})([0][2])([0][1-9]|[1][\d]|[2][0-8]))(\w{2}[A|a|0-9]{1})$")
    '    ElseIf (sTipo = "M") Then
    '        Return Regex.IsMatch(sRFC, "^[a-zA-Z&ñÑ]{3,3}(([0-9]{2})([0][13456789]|[1][012])([0][1-9]|[12][\d]|[3][0])|([0-9]{2})([0][13578]|[1][02])([0][1-9]|[12][\d]|[3][01])|([02468][048]|[13579][26])([0][2])([0][1-9]|[12][\d])|([0-9]{2})([0][2])([0][1-9]|[1][\d]|[2][0-8]))(\w{2}[A|a|0-9]{1})$")
    '    ElseIf (sTipo = "T") Then
    '        Return Regex.IsMatch(sRFC, "^[a-zA-Z&ñÑ]{3,4}(([0-9]{2})([0][13456789]|[1][012])([0][1-9]|[12][\d]|[3][0])|([0-9]{2})([0][13578]|[1][02])([0][1-9]|[12][\d]|[3][01])|([02468][048]|[13579][26])([0][2])([0][1-9]|[12][\d])|([0-9]{2})([0][2])([0][1-9]|[1][\d]|[2][0-8]))(\w{2}[A|a|0-9]{1})$")
    '    Else
    '        Return False
    '    End If

    '    '' ''If (sTipo = "F") Then
    '    '' ''    Return Regex.IsMatch(sRFC, "^[a-zA-ZñÑ]{4,4}(\d{6})((\D|\d){3})$")
    '    '' ''ElseIf (sTipo = "M") Then
    '    '' ''    Return Regex.IsMatch(sRFC, "^[a-zA-Z&ñÑ]{3,3}(\d{6})((\D|\d){3})$")
    '    '' ''ElseIf (sTipo = "T") Then
    '    '' ''    Return Regex.IsMatch(sRFC, "^[a-zA-Z&ñÑ]{3,4}(\d{6})((\D|\d){3})$")
    '    '' ''Else
    '    '' ''    Return False
    '    '' ''End If
    'End Function

#Region "KeyPress"
    Public Shared Sub sendTab(ByVal ctrlOrigen As System.Object, ByVal evnt As System.Windows.Forms.KeyPressEventArgs)
        'Pasa al siguiente control presionando Enter
        If (evnt.KeyChar = ChrW(Keys.Enter)) Then
            evnt.Handled = True
            SendKeys.Send("{TAB}")
        Else
            evnt.Handled = False
        End If
    End Sub

    Public Shared Sub onlyNumberAndLetter(ByVal ctrlOrigen As System.Object, ByVal evnt As System.Windows.Forms.KeyPressEventArgs)
        If (evnt.KeyChar = ChrW(Keys.Enter)) Then
            evnt.Handled = True
            SendKeys.Send("{TAB}")
        Else
            If (Char.IsLetterOrDigit(evnt.KeyChar)) Then
                evnt.Handled = False
            ElseIf (Char.IsControl(evnt.KeyChar)) Then
                evnt.Handled = False
            Else
                evnt.Handled = True
            End If
        End If
    End Sub

    Public Shared Sub onlyNumbers(ByVal ctrlOrigen As System.Object, ByVal evnt As System.Windows.Forms.KeyPressEventArgs)
        If (evnt.KeyChar = ChrW(Keys.Enter)) Then
            evnt.Handled = True
            SendKeys.Send("{TAB}")
        Else
            If (Char.IsDigit(evnt.KeyChar)) Then
                evnt.Handled = False
            ElseIf (Char.IsControl(evnt.KeyChar)) Then
                evnt.Handled = False
            Else
                evnt.Handled = True
            End If
        End If
    End Sub
#End Region
End Class
